//
//  MZNaviHeader.swift
//  MZTool
//
//  Created by gby on 2022/3/9.
//

import UIKit

open class MZNaviHeader: UIView {
    open var title: String?{
        set{titleLab.text = newValue}
        get{return titleLab.text}
    }
    open var textColor: UIColor?{
        set{titleLab.textColor = newValue}
        get{return titleLab.textColor}
    }
    private let titleLab = UILabel.init(17, color:UIColor.black, aligment:.center)
    private var leftItemViews: [MZNaviItemView] = []
    private var rightItemViews: [MZNaviItemView] = []
    
    public let bgimgv = UIImageView()
    public var itemSpace: CGFloat = 10
    public var itemPading = UIEdgeInsets.init(top: 0, left: 10, bottom: 4, right: 10)
    
    public var leftItem: MZNaviItemView? {
        didSet{
            if let a = leftItem {
                resettingLeftItems(views: [a])
            }
        }
    }
    public var rightItem: MZNaviItemView? {
        didSet{
            if let a = rightItem {
                resettingRightItems(views: [a])
            }
        }
    }
    
    public var leftItems: [MZNaviItemView]? {
        didSet{
            if let a = leftItems {
                resettingLeftItems(views: a)
            }
        }
    }
    
    public var rightItems: [MZNaviItemView]? {
        didSet{
            if let a = rightItems {
                resettingRightItems(views: a)
            }
        }
    }
    
    public convenience init(_ title: String = "") {
        self.init()
        self.title = title
        
        frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: MZ.Navi_Height())
        backgroundColor = UIColor.white
        
        rearrangement()
    }
    
    
    
    func rearrangement(){
        addSubview(bgimgv)
        addSubview(titleLab)
        
        bgimgv.frame = CGRect.init(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        let size = NSString(string: titleLab.text ?? "").boundingRect(
            with: CGSize.init(width: UIScreen.main.bounds.width - 100, height: titleLab.font.lineHeight),
            options: .usesLineFragmentOrigin,
            attributes: [NSAttributedString.Key.font:titleLab.font!],
            context: nil)
        
        titleLab.frame = CGRect.init(x: 0, y: 0, width: size.width, height: titleLab.font.lineHeight)
        titleLab.center = CGPoint.init(x: self.frame.width/2, y: (MZ.Navi_Height() + MZ.Navi_State_Height())/2)
    }
    
    private func resettingLeftItems(views: [MZNaviItemView]) {
        while leftItemViews.count > 0 {
            let view = leftItemViews.last
            view?.removeFromSuperview()
            leftItemViews.removeLast()
        }
        var left: CGFloat = 0
        for (_, view) in views.enumerated() {
            view.frame = CGRect(x: (left == 0 ? itemPading.left : itemSpace) + left, y: self.mz.height - view.mz.height - itemPading.bottom, width: view.mz.width, height: view.mz.height)
            self.addSubview(view)
            leftItemViews.append(view)
            left = view.mz.right
        }
    }
    
    private func resettingRightItems(views: [MZNaviItemView]) {
        while rightItemViews.count > 0 {
            let view = rightItemViews.last
            view?.removeFromSuperview()
            rightItemViews.removeLast()
        }
        
        var right: CGFloat = 0
        for (_, view) in views.enumerated().reversed() {
            view.frame = CGRect(x: self.mz.width - (right == 0 ? itemPading.right : itemSpace) - view.mz.width - right, y: self.mz.height - view.mz.height - itemPading.bottom, width: view.mz.width, height: view.mz.height)
            self.addSubview(view)
            rightItemViews.append(view)
            right = self.mz.width - view.mz.left
        }
    }
    
}

public class MZNaviItemView: UIView {
    private var lab: UILabel?
    private var imagev: UIImageView?
    
    public convenience init(size:CGSize,
                                     titleAStr: NSMutableAttributedString?,
                                     image: UIImage?,
                                     target: Any? = nil,
                                     selector: Selector? = nil) {
        self.init()
        frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        
        if let title = titleAStr {
            lab = UILabel.init()
            addSubview(lab!)
            lab?.attributedText = title
        }
        if let img = image {
            imagev = UIImageView.init()
            addSubview(imagev!)
            imagev?.image = img
        }
        addTap(target: target, selector: selector)
    }
    
    public convenience init(size:CGSize,
                                     customView: UIView?,
                                     target: Any? = nil,
                                     selector: Selector? = nil) {
        self.init()
        frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        
        if let cus = customView {
            addSubview(cus)
        }
        addTap(target: target, selector: selector)
    }
    
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        if lab == nil, imagev != nil {
            imagev?.frame = CGRect(x: 0, y: 0, width: imagev?.image?.size.width ?? 0, height: imagev?.image?.size.height ?? 0)
            imagev?.center = CGPoint(x: self.mz.width/2, y: self.mz.height/2)
        }
        else if lab != nil, imagev == nil {
            lab?.sizeToFit()
            lab?.center = CGPoint(x: self.mz.width/2, y: self.mz.height/2)
        }
        else if lab != nil, imagev != nil {
            lab?.sizeToFit()
            imagev?.frame = CGRect(x: 0, y: 0, width: imagev?.image?.size.width ?? 0, height: imagev?.image?.size.height ?? 0)
            let width = lab!.mz.width
            let height = lab!.mz.height
            imagev?.center = CGPoint(x: self.mz.width/2, y: self.mz.height/2 - 4 - height/2)
            lab?.frame = CGRect(x: 0, y: imagev!.mz.bottom + 4, width: width, height: height)
        }
    }
    
    public func addTap(target: Any?, selector: Selector?){
        guard target != nil && selector != nil else {
            return
        }
        isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: target, action: selector)
        self.addGestureRecognizer(tap)
    }
}
